<?php

//<editor-fold defaultstate="collapsed" desc="License & Copyright">
/*
 * @license
 */

/**
 *
 * @author Jevgeni Geimanen (jevgeni.geimanen@gmail.com)
 */
//</editor-fold>
class SCConfig extends SCStruct {

    //<editor-fold defaultstate="collapsed" desc="Ini">
    /**
     * @var array
     */
    private $_Ini = null;

    /**
     *
     * @return array
     */
    public function getIni() {
        return $this->_Ini;
    }

    /**
     *
     * @param array $ini
     * @return SCConfig 
     */
    public function setIni($ini) {
        $this->_Ini = $ini;
        return $this;
    }

    //</editor-fold>
    public function getSection($name) {
        return is_array($this->_Ini) && isset($this->_Ini[$name]) ? $this->_Ini[$name] : null;
    }
    public function init($ini) {
        $this->setIni($ini);
        foreach ($ini as $key => $val) {
            if (method_exists($this, $method_name = "set" . ucfirst($key) . "Config")) {
                if (class_exists($className = ucfirst($key) . "Config")) {
                    $this->{$method_name}($className::fromIni($val));
                }
            } else if (method_exists($this, $method_name = "set" . ucfirst($key))) {
                $this->{$method_name}($val);
            }
        }
    }

    //<editor-fold defaultstate="collapsed" desc="Include">
    /**
     * @var array
     */
    private $_Include = null;

    /**
     *
     * @return array
     */
    public function getInclude() {
        return $this->_Include;
    }

    /**
     *
     * @param array $include
     * @return Include 
     */
    public function setInclude($include) {
        $this->_Include = $include;
        return $this;
    }

//</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Var">
    /**
     * @var array
     */
    private $_Var = null;

    /**
     *
     * @return array
     */
    public function getVar() {
        return $this->_Var;
    }

    /**
     *
     * @param array $var
     * @return Config
     */
    public function setVar($var) {
        $this->_Var = $var;
        return $this;
    }

//</editor-fold>
    
    public function getIncludesAbsolute($suffix) {
        $subpaths = array(SCORE_PATH, SCORE_APP_LIB_PATH);
        $res = array();
        $paths = self::getInclude();
        foreach ($paths as $path)
            foreach ($subpaths as $subpath)
                if (file_exists($pp = "{$subpath}{$suffix}/{$path}"))
                    $res[$pp] = 1;
        return $res;
    }

    public static function fromIni($ini) {
        $config = get_called_class();
        $res = new $config();
        $res->init($ini);
        return $res;
    }

    public static function fromIniFile($file) {
        return static::fromIni(SCIniHelper::parseIniFile($file));
    }

}

?>
